package com.canbot.u05.activity.presidemode.newcode;

import android.app.Dialog;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.activity.presidemode.newcode.bean.NModuleType;
import com.canbot.u05.activity.presidemode.newcode.bean.NetLoadUtil;
import com.canbot.u05.activity.presidemode.newcode.bean.VoiceBean;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IHeadCommonClick;
import com.canbot.u05.activity.presidemode.newcode.interfaces.IResponse;
import com.canbot.u05.activity.presidemode.newcode.view.HeadCommonView;
import com.canbot.u05.activity.presidemode.newcode.view.ItemChoiceView;
import com.canbot.u05.activity.presidemode.newcode.view.NGuideRecordDialog;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import butterknife.BindView;

public class NVoiceChoiceSettingsActivity extends BaseHostActivity implements View.OnClickListener {
    @BindView(R.id.headCommonView)
    HeadCommonView headCommonView;
    @BindView(R.id.item_choice_none)
    ItemChoiceView itemChoiceNone;
    @BindView(R.id.item_choice_music)
    ItemChoiceView itemChoiceMusic;
    @BindView(R.id.item_choice_tts)
    ItemChoiceView itemChoiceTts;
    @BindView(R.id.item_choice_record)
    ItemChoiceView itemChoiceRecord;
    private NGuideRecordDialog guideRecordDialog;
    private VoiceBean voiceBean;

    @Override
    protected void initEventData() {
        MapVoiceAndEyes.playMapVoiceAndEyes(MapVoiceAndEyes.voiceEyesList.get(21));
        initCommonHead();
        itemChoiceNone.setOnClickListener(this);
        itemChoiceMusic.setOnClickListener(this);
        itemChoiceTts.setOnClickListener(this);
        itemChoiceRecord.setOnClickListener(this);
        initIntent();
    }

    private void initIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            voiceBean = (VoiceBean) intent.getSerializableExtra(DATA);
            if (voiceBean != null) {
                switch (voiceBean.languageType) {
                    case NModuleType.VOICE_MUSIC:
                        itemChoiceMusic.select();
                        break;
                    case NModuleType.VOICE_TTS:
                        itemChoiceTts.select();
                        break;
                    case NModuleType.VOICE_NONE:
                        itemChoiceNone.select();
                        break;
                    case NModuleType.VOICE_RECORD:
                        itemChoiceRecord.select();
                        break;
                }
            } else {
                itemChoiceNone.select();
            }
        } else {
            itemChoiceNone.select();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MapVoiceAndEyes.stop();
    }

    private void initCommonHead() {
        headCommonView.setiHeadCommonClick(pos -> {
            switch (pos) {
                case IHeadCommonClick.LEFT:
                    finish();
                    break;
                case IHeadCommonClick.RIGHT:
                    if (itemChoiceNone.isSelect()) {
                        sendData(new VoiceBean(NModuleType.VOICE_NONE));

                    } else if (itemChoiceMusic.isSelect()) {
                        sendData(voiceBean);

                    } else if (itemChoiceTts.isSelect()) {
                        sendData(voiceBean);

                    } else if (itemChoiceRecord.isSelect()) {
                        sendData(voiceBean);
                    } else {
                        Toast.makeText(this, getString(R.string.music_null_tip), Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        });
    }

    private void sendData(VoiceBean voiceBean) {
        Log.e(TAG, "sendData: " + voiceBean);
        Intent intent = new Intent();
        intent.putExtra(DATA, voiceBean);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_nvoice_choice_settings;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.item_choice_none:
                updateSelectState(itemChoiceNone);
                break;
            case R.id.item_choice_music:
                Intent intent = new Intent(this, NChoiceMusicActivity.class);
                intent.putExtra(DATA, voiceBean);
                gotoActivityForResult(intent, NModuleType.VOICE_MUSIC);
                break;
            case R.id.item_choice_tts:
                intent = new Intent(this, NChoiceTtsActivity.class);
                intent.putExtra(DATA, voiceBean);
                gotoActivityForResult(intent, NModuleType.VOICE_TTS);
                break;
            case R.id.item_choice_record:
                record();
                break;
        }
    }

    private void updateSelectState(ItemChoiceView view) {
        itemChoiceNone.cancle();
        itemChoiceMusic.cancle();
        itemChoiceTts.cancle();
        itemChoiceRecord.cancle();
        view.select();
    }

    private static final String TAG = "NVoiceChoiceSettingsAct";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            switch (requestCode) {
                case NModuleType.VOICE_MUSIC:
                    updateSelectState(itemChoiceMusic);
                    voiceBean = (VoiceBean) data.getSerializableExtra(DATA);
                    voiceBean.languageType = NModuleType.VOICE_MUSIC;
                    break;
                case NModuleType.VOICE_TTS:
                    updateSelectState(itemChoiceTts);
                    voiceBean = (VoiceBean) data.getSerializableExtra(DATA);
                    voiceBean.languageType = NModuleType.VOICE_TTS;
                    break;
            }
        }
    }

    /**
     * 进入录音
     */
    private void record() {
        if (guideRecordDialog == null) {
            //点击语音录入图标
            guideRecordDialog = new NGuideRecordDialog(this, new NGuideRecordDialog.IRecodeListener() {
                @Override
                public void finish(String fileName, int time) {
                    if (TextUtils.isEmpty(fileName)) {
                        ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.recoding_null));
                        return;
                    }
                    Logger.d(TAG, "录音的文件名：" + fileName);
                    AudioEvent recodeAudio = new AudioEvent(AudioEvent.AUDIO_TYPE_RECODE, PathConst.RECORD_FILE_PATH + fileName, fileName);
                    recodeAudio.setTime(time);
                    updateload(recodeAudio);
                }

                @Override
                public void cancel() {

                }

                @Override
                public void error() {
                    ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.recoding_error));
                }
            });
        }
        guideRecordDialog.show();
    }

    private Dialog dialogLoading;

    private void updateload(AudioEvent recodeAudio) {
        dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.data_upload));
        NetLoadUtil.updateRecordFile(getApplicationContext(), new IResponse<AudioEvent>() {
            @Override
            public void onSucusse(AudioEvent result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                voiceBean = new VoiceBean();
                voiceBean.content = result.getMusicName();
                voiceBean.languageType = NModuleType.VOICE_RECORD;
                voiceBean.path = PathConst.RECORD_FILE_PATH1+ result.getMusicName();
                voiceBean.time = result.getTime();
                ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.upload_complete));
                runOnUiThread(() -> updateSelectState(itemChoiceRecord));
            }

            @Override
            public void onFailuer(String result) {
                LoadingDialogUtils.closeDialog(dialogLoading);
                ToastTools.newInstance(getApplicationContext()).showText(getString(R.string.upload_failed));
            }
        }, recodeAudio);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LoadingDialogUtils.closeDialog(dialogLoading);
    }
}
